<?php
/**
 * @package JoiCaddy
 * @copyright Авторские права (C) 2008-2010 Joostina team. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * JoiCaddy - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * JoiCaddy основан на разработках SimpleCaddy от Henk von Pickartz
 */

defined('_VALID_MOS') or die();
$debug=0;
DEFINED ('JPATH_COMPONENT') or DEFINE ('JPATH_COMPONENT', dirname(__FILE__));

require_once( $mainframe->getPath( 'admin_html' ) );
require_once( $mainframe->getPath( 'class' ) );
require_once( JPATH_COMPONENT.DS.'admin.shipping.html.php' );

global $mosConfig_lang;
$lang = ($mosConfig_lang == '') ? 'russian' : $mosConfig_lang;
require_once( JPATH_COMPONENT.DS.'lang'.DS. $lang .'.php' );
//JPlugin::loadLanguage( 'com_simplecaddy' );

$action = mosGetParam($_REQUEST, 'action' );
$task = mosGetParam($_REQUEST, 'task', 'show' );
$cid = mosGetParam($_REQUEST,  'cid', array());

if (($task=="control")) {
	// simple system check
	$db	= database::getInstance();
	$query="select * from #__plugins where name='simplecaddy'";
	$db->setQuery($query)->loadObject($scplugin);
	$message="";
	if (!$scplugin) {
		$message .= SC_PLUGIN_NOT_INSTALLED;
	}
	else
	{
		if ($scplugin->published=='0') {
			$message .= SC_PLG_NOT_PUBLISHED;
		}
	}
	$query="select * from #__modules where module='mod_caddy'";
	$db->setQuery($query)->loadObject($scmodule);
	if ($scmodule) {
		if ($scmodule->published=='0') {
			$message .= SC_MOD_NOT_PUBLISHED;
		}
	}
	
	display::MainMenu($message);
	if ($debug) echo "<p>Development debug info</p><strong>task= '$task'<br>action='$action'</strong>";
	if (!mosGetParam($_REQUEST, 'no_html')) {
		display::afFooter();
	}
	return;
}

switch ($action) {
case "checksums":
	$here=JPATH_COMPONENT_SITE;
	$folder1=JFolder::files($here, null, true, true);
	$here=JPATH_COMPONENT_ADMINISTRATOR;
	$folder2=JFolder::files($here, null, true, true, array("checksums.php"));
	$here=JPATH_PLUGINS.DS."content";
	$folder3=JFolder::files($here, "caddy", true, true);
	$folder=array_merge($folder1, $folder2, $folder3);
	echo "<table border='1'><tr><th>File</th><th>Checksum</th></tr>";
	foreach ($folder as $key=>$file) {
		echo "<tr>";
		echo "<td>\$checksums['".$file."']=</td>";
		$md5=md5_file($file);
		echo "<td>'".$md5."';</td>";
		echo "</tr>";
	}
	echo "</table>";
	break;	
case "orders":
	$search=mosGetParam($_REQUEST, "search");
	$field=mosGetParam($_REQUEST, "field");
	$order=mosGetParam($_REQUEST, "order");
	switch ($task) {
	case "apply":
	case "save":
		$cid[0]=mosGetParam($_REQUEST, "id");
		$_REQUEST['task'] = "orders";
		$tmp=new Orders();
		$tmp->saveorder();
		if ($task=="save")
			mosRedirect("index2.php?option=com_caddy&action=orders&task=show&field=$field&order=$order&search=$search", SC_ORDERSAVED);
	case "edit":
	case "view":
		$tmp= new orders();
		$order=$tmp->getorder($cid[0]);
		$details=$tmp->getODetails($cid[0]);
		display::editOrder($order, $details['lst'], $details['nav']);
		break;
	case "remove":
		$_REQUEST['task'] =  "orders";
		$tmp=new Orders();
		$tmp->removeOrders($cid);
		mosRedirect("index2.php?option=com_caddy&action=orders&task=show&field=$field&order=$order&search=$search", SC_ORDERDELETED);
		break;
	case "export":
		$order=new order();
		$csvline=$order->ordertostring($cid);
		$_REQUEST['task'] = "about";
		display::showExport();
		break;
	case "email":
	printf("<pre>%s</pre>", print_r($_REQUEST, 1));
		$oid=mosGetParam($_REQUEST, "oid");
		$email=new email();
		$email->mailorder($oid);
		mosRedirect("index2.php?option=com_caddy&action=orders&task=edit&id=$oid&field=$field&order=$order&search=$search", SC_EMAIL_SENT);
		break;
	case "viewarchive":
		$_REQUEST['task'] = "orders";
		$a=new Orders();
		$alist=$a->getAllOrders($field, $order, null, $search, 1);
		display::ShowOrders($alist, $field, $order);
		break;
	case "show":
	default:
		$_REQUEST['task'] = "orders";
		$a=new Orders();
		$alist=$a->getAllOrders($field, $order, null, $search);
		display::ShowOrders($alist, $field, $order);
		break;
	}
	break;
case "products":
	$search=mosGetParam($_REQUEST, "search");
	$field=mosGetParam($_REQUEST, "field");
	$order=mosGetParam($_REQUEST, "order");
	switch ($task) {
		case "apply":
			$tmp=new products();
			$tmp->saveproduct();
			$cid[0]=$tmp->id;
		case "new":
		case "edit":
			$p= new products();
			$p->getproduct(@$cid[0]);
			display::editProduct($p);
			break;
		case "duplicate":
			$p= new products();
			$p->getproduct($cid[0]);
			$p->id=null; // force new record
			$p->store();
			//$_REQUEST['task']="edit";
			display::editProduct($p);
			break;
		case "save":
			$tmp=new products();
			$tmp->saveproduct();
			mosRedirect("index2.php?option=com_caddy&action=products&task=show&field=$field&order=$order&search=$search", SC_PRODUCTSAVED);
			break;
		case "decstore":
			$pid=mosGetParam($_REQUEST, 'pid', '' );
			$qty=mosGetParam($_REQUEST, 'qty', '' );
			$oid=mosGetParam($_REQUEST, 'order', '' );
			$tmp=new products();
			$tmp->decfromstore($pid, $qty);
	
			//now get back to this order in edit/view mode
			//$_REQUEST['task']="edit";
			$tmp= new orders();
			$order=$tmp->getorder($oid);
			$details=$tmp->getODetails($oid);
			display::editOrder($order, $details['lst'], $details['nav']);
			break;
		case "remove":
			$tmp=new products();
			$tmp->removeProducts($cid);
			mosRedirect("index2.php?option=com_caddy&action=products&task=show&field=$field&order=$order&search=$search", SC_PRODUCTDELETED);
			break;
		case "publish":
		case "unpublish":
			$a=new products();
			$a->publishproduct($cid, ($task == 'publish'));
			mosRedirect("index2.php?option=com_caddy&action=products&task=show&field=$field&order=$order&search=$search");
			break;
		case "show":
		default:
			//$_REQUEST['task']="products";
			$a=new products();
			$alist=$a->getAllProducts($search, $field, $order);
			display::ShowProducts($alist, $field, $order);
			break;
		}
	break;
case "fields":
	$search=mosGetParam($_REQUEST, "search");
	$field=mosGetParam($_REQUEST, "field");
	$order=mosGetParam($_REQUEST, "order");
	switch ($task) {
		case "apply":
			$fields=new fields();
			$fields->savefield();
			$cid[0]=$fields->id;
		case "add":
		case "new":
		case "edit":
			$fields= new fields();
			$fields->getfield($cid[0]);
			display::editField($fields);
			break;
		case "save":
			$fields=new fields();
			$fields->saveField();
			mosRedirect("index2.php?option=com_caddy&action=fields&task=show&field=$field&order=$order&search=$search", SC_FIELDSAVED);
			break;
		case "remove":
			$fields=new fields();
			$fields->removeFields($cid);
			mosRedirect("index2.php?option=com_caddy&action=fields&task=show&field=$field&order=$order&search=$search", SC_FIELDDELETED);
			break;
		case "publish":
		case "unpublish":
			$fields=new fields();
			$fields->publishfield($cid, ($task == 'publish'));
			mosRedirect("index2.php?option=com_caddy&action=fields&task=show&field=$field&order=$order&search=$search");
			break;
		case "show":
		default:
			$_REQUEST['task']="fields";
			$fields=new fields();
			$alist=$fields->getAllFields();
			display::showFields($alist);
			break;
		}
	break;
case "vouchers":

    $cid = mosGetParam($_REQUEST, 'cid');

	switch($task){

	case 'save':
        $voucher=new voucher();
        $voucher->savevoucher();
        mosRedirect("index2.php?option=com_caddy&action=vouchers&task=show");
		break;

	case 'new':
	case 'edit':
        $voucher=new voucher();
		$v=$voucher->getvoucher($cid[0]);
		display::editVoucher($v);
		break;

	case 'publish':
        $voucher=new voucher();
        $voucher->publishvoucher($cid, 1);
        mosRedirect("index2.php?option=com_caddy&action=vouchers&task=show");
        break;

    case 'unpublish':
        $voucher=new voucher();
        $voucher->publishvoucher($cid, 0);
        mosRedirect("index2.php?option=com_caddy&action=vouchers&task=show");
        break;

	case 'remove':
        $voucher=new voucher();
        $voucher->removevouchers($cid);
        mosRedirect("index2.php?option=com_caddy&action=vouchers&task=show");
		break;

	case 'show':
	default:
        $field = mosGetParam($_REQUEST, 'field', '');
        $order = mosGetParam($_REQUEST, 'order', '');
        $voucher=new voucher();
		$v=$voucher->getAllvouchers();
		display::showVouchers($v, $field, $order);
		break;

	}
	break;

case "shipping":
	switch($task){
	case 'save':
		$_REQUEST['task']="shipping";
		//shipping::doViewShippingZones();
		$msg = shipping::saveShipping();
		mosRedirect("index2.php?option=com_caddy&action=shipping&task=show", $msg);
		break;
	case 'add':
	case 'edit':
		$row=shipping::prepareEditShipping($cid);
		shipping_HTML::editShipping($row);		
		break;
	case 'remove':
		$_REQUEST['task']="shipping";
		shipping::doViewShippingZones(shipping::deleteZone($cid));
		break;
	case 'show':
	default:
		$_REQUEST['task']="shipping";
		shipping::doViewShippingZones();
		break;
		
	}
	break;
case "configuration":
	switch ($task) {
	case "save":
		$cfgset=mosGetParam($_REQUEST, 'cfgset');
		$_REQUEST['task']="configuration";
		$cfg=new sc_configuration($cfgset);
		$cfg->setAll();
		mosRedirect("index2.php?option=com_caddy&action=configuration&task=show", "Configuration saved");
	case "cancel":
		mosRedirect("index2.php?option=com_caddy&action=configuration&task=show", "Reverting to previous Configuration");
	default:
		$cfgset=mosGetParam($_REQUEST, 'cfgset');
		$_REQUEST['task']="configuration";
		$cfg=new sc_configuration($cfgset);
		display::header(SC_CONFIGURATION);
		$cfg->show();
		break;
	}
	break;
case "view_prod":
	$a=new products();
	$alist=$a->getPublishedProducts();
	display::view_prod($alist);
	break;
case "about":
	switch ($task) {
	default:
		$_REQUEST['task']="about";
		display::ShowAbout();
		break;
	}
	break;
default:
	$_REQUEST['task']="control";
	display::MainMenu();
}


if ($debug) echo "<p>Development debug info</p><strong>task= '$task'<br>action='$action'</strong>";
if (!mosGetParam($_REQUEST, 'no_html')) {
	display::afFooter();
}

?>
